#准备工作
# 导入库
import pygame
from pygame.locals import *
import math
import random

        
def wujin():

	# 初始化pygame并设置显示窗口
	
	pygame.init()
	screen = pygame.display.set_mode((640,480))
	key_tag =[False,False,False,False]
	dudepos =[100,90]
	arrows = []
	timer = random.randint(10,80)
	badguys = []
	blood_down = 0
	blood_up = 0
	acc = [0,0] # 1 射中   2  射出
	running = True

	# 加载要使用的图片
	dude = pygame.image.load('dude.png')
	grass = pygame.image.load('grass.png')
	castle = pygame.image.load('castle.png')
	bullet = pygame.image.load('bullet.png')
	badguy1 = pygame.image.load('badguy.png')
	badguy2 = pygame.image.load('badguy2.png')
	badguy3 = pygame.image.load('badguy3.png')
	badguy4 = pygame.image.load('badguy4.png')
	all_badguys = [badguy1,badguy2,badguy3,badguy4]
	n = 0
	redbar = pygame.image.load('healthbar.png')
	greenbar = pygame.image.load('health.png')
	lose = pygame.image.load('gameover.png')
	win = pygame.image.load('youwin.png')

	# 加载声音
	pygame.mixer.music.load('moonlight.wav')
	pygame.mixer.music.set_volume(1)
	pygame.mixer.music.play(-1,0.0)


	# 加载音效
	shoot = pygame.mixer.Sound('shoot.wav')
	shoot.set_volume(1)


	for x in range(7):
		for y in range(5):
			screen.blit(grass,(x * 100,y * 100))
	for i in range(4):
		screen.blit(castle,(0,60 + i*(100)))

	# 实时监测事件并做出相应处理
	while running:
		timer -= 1
		for event in pygame.event.get():
			# 监测并处理退出事件
			if event.type == QUIT:
				pygame.quit()
				exit(0)

			# 监测按键事件并设置相应标记变量值以确认按键时长
			elif event.type == KEYDOWN:
				if event.key == K_w:
					key_tag[0] = True
				elif event.key == K_a:
					key_tag[1] = True
				elif event.key == K_s:
					key_tag[2] = True
				elif event.key == K_d:
					key_tag[3] = True
					
			elif event.type == KEYUP:
				if event.key == K_w:
					key_tag[0] = False
				elif event.key == K_a:
					key_tag[1] = False
				elif event.key == K_s:
					key_tag[2] = False
				elif event.key == K_d:
					key_tag[3] = False

			elif event.type == MOUSEBUTTONDOWN:
				
				x1 = dudepos[0] + dude.get_width()/2  #兔子中心点
				y1 = dudepos[1] + dude.get_height()/2
				
				x2 = pygame.mouse.get_pos()[0]  #鼠标
				y2 = pygame.mouse.get_pos()[1]
				
				angle_1 = math.atan2(y2-y1,x2-x1)
				
				arrows.append([angle_1,x1,y1])

				acc[1] += 1

				shoot.play()
			
			   
		# 根据标记变量值响应按键事件
		step = 10
		if key_tag[0] == True:
			dudepos[1] -= step
		elif key_tag[1] == True:
			dudepos[0] -= step
		elif key_tag[2] == True:
			dudepos[1] += step
		elif key_tag[3] == True:
			dudepos[0] += step


		# 放置各图片对象
		screen.fill(0)
		castle = pygame.image.load('castle.png')
		grass = pygame.image.load('grass.png')
		
		x1 = dudepos[0] + dude.get_width()/2  #兔子中心点
		y1 = dudepos[1] + dude.get_height()/2
		x2 = pygame.mouse.get_pos()[0]  #鼠标
		y2 = pygame.mouse.get_pos()[1]
		angle_1 = math.atan2(y2-y1,x2-x1)
		angle_2 = 360 * angle_1 / (2 * math.pi)  #角度
		duderot = pygame.transform.rotate(dude,-angle_2)
		x_duderot = x1 - duderot.get_width()/2
		y_duderot = y1 - duderot.get_height()/2
		duderotpos = (x_duderot,y_duderot)
		
		# 草坪
		for x in range(7):
			for y in range(5):
				screen.blit(grass,(x * 100,y * 100))

				
		# 兔子窝
		for i in range(4):
			screen.blit(castle,(0,60 + i*(100)))
		

		# 兔子（有更新位置）
		screen.blit(duderot,(duderotpos))

		step_b = 10
		removed_arrows = []
		for arrow in arrows:
			arrow[1] += step_b*math.cos(arrow[0])
			arrow[2] += step_b*math.sin(arrow[0])
			if (arrow[1]>screen.get_width() or arrow[2]>screen.get_height() or
				arrow[1]<-bullet.get_width() or arrow[2]<-bullet.get_height()):
					removed_arrows.append(arrow)
		

		


		#生成并显示badguy
		if timer == 0:
			x_badguy = screen.get_width()
			y_badguy = random.randint(80,screen.get_height())
			badguys.append([x_badguy,y_badguy])
			timer = random.randint(30,50)


		step_badguy = 5
		removed_badguys = []
		for guy in badguys:
			guy[0] -= step_badguy
			if guy[0] < castle.get_width():
				blood_down += 20
				removed_badguys.append(guy)
			else:
				for arrow in arrows:
					guyrect = badguy1.get_rect()
					guyrect.left = guy[0]
					guyrect.top = guy[1]


					arrowrect = bullet.get_rect()
					arrowrect.left = arrow[1]
					arrowrect.top = arrow[2]

					if arrowrect.colliderect(guyrect):
						blood_up += 0
						removed_arrows.append(arrow)
						removed_badguys.append(guy)
						acc[0] += 1

		 #在arrow中删除超出屏幕范围的箭               
		for arrow in removed_arrows:
			try:
				arrows.remove(arrow)
			finally:
				pass


		#显示箭    
		for arrow in arrows:
			bulletrot = pygame.transform.rotate(bullet,-arrow[0]*360/(2*math.pi))
			screen.blit(bulletrot,(arrow[1],arrow[2]))

		for guy in removed_badguys:
			badguys.remove(guy)

		for guy in badguys:
			screen.blit(all_badguys[n%4],guy)

		n += 1

		
		# 计算命中率
		if acc[1] != 0:
			acc_text = str(acc[0] * 100 / acc[1])+'%'
		else:
			acc_text= '0' + '%'

		font_acc = pygame.font.SysFont('Inkfree',30)


		acc_image = font_acc.render(acc_text,True,(0,0,0))
		 

		

		


		
		# 更新屏幕
		pygame.display.flip()

	while 1:
		for event in pygame.event.get():
			# 监测并处理退出事件
			if event.type == QUIT:
				pygame.quit()
				exit(0)













